﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyLift.TMS.Web.Models;
using EasyLift.TMS.Web.Helpers;

namespace CoreBiz.Web.Controllers
{
    public class MissionHeaderController : Controller
    {

        MissionHeaderRepository repository = new MissionHeaderRepository();
        //
        // GET: /MissionHeader/

        public ActionResult Index(int? page)
        {
            int pageSize = 10;
            var MissionHeaders = repository.GetAllData();
            var paginatedMissionHeaders = new PaginatedList<MissionHeader>(MissionHeaders, page ?? 0, pageSize);
            return View("Index", paginatedMissionHeaders);
        }

        public JsonResult GridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
        {

            var allData = repository.GetAllData(sidx, sord, _search, searchField, searchOper, searchString);

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = allData.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var paginatedAllData = new PaginatedList<MissionHeader>(allData, pageIndex, pageSize);


            var JsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from p in paginatedAllData
                    select new
                    {
                        id = p.MissionID.ToString(),
                        cell = new string[] {
                            p.MissionID.ToString().ToString(),
                            p.TMR,
                            p.MissionDate.ToString(),
                            p.MissionType.MissionTypeDescription,
                            p.MissionDetails.Count.ToString(),
                            p.MissionStatus.MissionStatusDescription
                        }
                    }
                ).ToArray()
            };

            return Json(JsonData, JsonRequestBehavior.AllowGet);
        }



        //
        // GET: /MissionHeader/Details/5

        public ActionResult Details(long id)
        {
            MissionHeader MissionHeader = repository.GetByID(id);
            if (MissionHeader == null)
                return View("NotFound");
            else
                return View("Details", MissionHeader);
        }

        //
        // GET: /MissionHeader/Create

        public ActionResult Create()
        {
            MissionHeader pf = new MissionHeader();
            pf.TMR = "TMR-";
            pf.MissionStatusCode = "1";
            pf.MissionTypeCode = "1";
            return View(new MissionHeaderViewModel(pf));
        }

        //
        // POST: /MissionHeader/Create

        [HttpPost]
        public ActionResult Create(MissionHeader MissionHeader)
        {

            //UpdateModel(MissionHeader);
            try
            {
                if (String.IsNullOrEmpty(MissionHeader.TMR))
                {
                    ModelState.AddModelError("TMR", "TMR is required");
                }

                if (ModelState.IsValid)
                {

                    repository.Add(MissionHeader);
                    repository.Save();
                }
                else
                {
                    return View(new MissionHeaderViewModel(MissionHeader));
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(new MissionHeaderViewModel(MissionHeader));
            }

        }

        //
        // GET: /MissionHeader/Edit/5

        public ActionResult Edit(long id)
        {
            try
            {
                MissionHeader MissionHeader = repository.GetByID(id);
                if (MissionHeader == null)
                    return View("NotFound");

                return View(new MissionHeaderViewModel(MissionHeader));
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /MissionHeader/Edit/5

        [HttpPost]
        public ActionResult Edit(long id, MissionHeader MissionHeader)
        {
            try
            {
                //UpdateModel(MissionHeader);
                repository.Update(MissionHeader);
                repository.Save();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DataVersionConflict", ex);

                return View(new MissionHeaderViewModel(MissionHeader));

            }

        }

        //
        // GET: /MissionHeader/Delete/5

        public ActionResult Delete(long id)
        {
            MissionHeader MissionHeader = repository.GetByID(id);

            return View(MissionHeader);
        }

        //
        // POST: /MissionHeader/Delete/5

        [HttpPost]
        public ActionResult Delete(long id, FormCollection collection)
        {
            try
            {
                MissionHeader MissionHeader = repository.GetByID(id);

                repository.Delete(MissionHeader);
                repository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

    }
}
